<?php


namespace app\wenchuang\controller;


use app\wenchuang\model\PolicyModel;
use app\wenchuang\model\TheoryModel;
use app\wenchuang\validate\PolicyValidate;
use app\wenchuang\validate\TheoryValidate;
use cmf\controller\AdminBaseController;

class PolicyController  extends AdminBaseController
{
    //显示列表
    public function index()
    {
        $title = $this->request->param('title');
        $startTime = $this->request->param('start/s');
        $endTime = $this->request->param('end/s');
        $status = $this->request->param('status/d');
        $where = [];
        if ($title) {
            $where[] = ['title', 'like', '%'.$title.'%'];
        }
        if (!empty($startTime) && empty($endTime)) {
            $where[] = ['create_time', '>=', strtotime($startTime)];
        }
        if (empty($startTime) && !empty($endTime)) {
            $where[] = ['create_time', '<=', strtotime($endTime)];
        }
        if (!empty($startTime) && !empty($endTime)) {
            $where[] = ['create_time', 'between time', [strtotime($startTime), strtotime($endTime)]];
        }
        if ($status != '') {
            $where[] = ['status', '=', $status];
        }
        $data = PolicyModel::order(['list_order' => 'desc'])
            ->where($where)
            ->paginate(10);
        // 追加请求参数，分页后仍然保留搜索条件
        $data->appends($this->request->param());
        // 得到分页按钮
        $page = $data->render();
        $this->assign('policy', $data);
        $this->assign('page', $page);
        return $this->fetch();
    }

    //排序
    public function listOrder()
    {
        parent::listOrders(new PolicyModel());
        $this->success('排序成功');
    }

    //显示添加页面
    public function add()
    {
        return $this->fetch();
    }

    //保存添加
    public function addpost()
    {
        $data = $this->request->param();

        $content = htmlspecialchars_decode($data['content']);
        $content = cmf_replace_content_file_url($content, true);
        $content = htmlspecialchars($content);
        $data['content'] = $content;
        $validate = new PolicyValidate();
        $result = $validate->scene('add')->check($data);
        if ($result !== true) {
            $this->error($validate->getError());
        }
        // 存库
        $PolicyModel = New PolicyModel();
        if ($PolicyModel->save($data)) {
            $this->success('添加成功', 'Policy/index');
        }
        $this->error('添加失败');
    }

    //显示修改页面
    public function edit()
    {
        $id = $this->request->param('id');
        $Policy = PolicyModel::find($id);
        //显示编辑页面也要处理存在数据库的富文本
        $Policy['content'] = cmf_replace_content_file_url(htmlspecialchars_decode($Policy['content']));
        $this->assign('policy', $Policy);
        return $this->fetch();
    }

    //保存修改
    public function editPost()
    {
        $data = $this->request->param();
        $content = htmlspecialchars_decode($data['content']);
        $content = cmf_replace_content_file_url($content, true);
        $content = htmlspecialchars($content);
        $data['content'] = $content;
        // 验证数据
        $validate = new PolicyValidate();
        $result = $validate->scene('add')->check($data);
        if ($result !== true) {
            $this->error($validate->getError());
        }

        // 编辑存库
        $PolicyModel = New PolicyModel();
        $Policy= $PolicyModel->where('id', '=', $data['id'])->find();
        $Policy->title = $data['title'];
        $Policy->abstract = $data['abstract'];
        $Policy->image = $data['image'];
        $Policy->content = $data['content'];
        $Policy->author = $data['author'];
        if ($Policy->save()) {
            $this->success('保存成功', 'Policy/index');
        }
        $this->error('保存失败');
    }

    //删除
    public function softDelete()
    {
        $id = input('id');
        $TheoryModel = New PolicyModel();
        // 查询要删除的数据
        $Policy= $TheoryModel->where('id', '=', $id)->find();
        if ($Policy->delete()) {
            $this->success('删除成功');
        }
        $this->error('删除失败');
    }
}